﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Globalization;
using System.Threading;

namespace INNOVACALL.Framework.Common.MVCHelpers.Localization
{
    public class InnovacallControllerFactory : DefaultControllerFactory 
    {
        protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType)
        {

            //Get the {language} parameter in the RouteData

            string UILanguage;
            //string FriendlyURL

            if (requestContext.RouteData.Values["language"] == null)
            {
                UILanguage = "fr";
            }
            else
            {
                UILanguage = requestContext.RouteData.Values["language"].ToString();
            }

            //if (requestContext.RouteData.Values["friendlyPath"] == null)
            //{
            //    FriendlyURL = string.Empty;
            //}
            //else
            //{
            //    FriendlyURL = requestContext.RouteData.Values["friendlyPath"].ToString();
            //}

            //Get the culture info of the language code
            CultureInfo culture = CultureInfo.CreateSpecificCulture(UILanguage);
            Thread.CurrentThread.CurrentCulture = culture;
            Thread.CurrentThread.CurrentUICulture = culture;

            return base.GetControllerInstance(requestContext, controllerType);
        }
    }
}
